package com.rx.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;

/**
 * 
 * @类名: CharacterEncodingFilter 
 * @描述: /* 过滤路径 
 * @作者：侯建军
 * @日期：2018年6月25日 下午3:55:29 
 *
 */
@WebFilter(filterName = "CharacterEncoding", urlPatterns = { "/*" }, initParams = {
		@WebInitParam(name = "encoding", value = "UTF-8") })
public class CharacterEncodingFilter implements Filter {

    public CharacterEncodingFilter() {
      
    }

	public void destroy() {
	
	}


	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		String encode=fConfig.getInitParameter("encoding");
		//设定请求编码
		request.setCharacterEncoding(encode);
		//设定响应编码
		response.setCharacterEncoding(encode);
		response.setContentType("text/html;charset=" + encode);
		chain.doFilter(request, response);
	}

	private FilterConfig fConfig;
	public void init(FilterConfig fConfig) throws ServletException {
		this.fConfig=fConfig;
	}

}
